<template>
  <ContentWrap>
    <el-descriptions border column="2">
      <el-descriptions-item label="活动名称" align="center" label-align="left" label-width="100">{{ list[0].activityName }}</el-descriptions-item>
      <el-descriptions-item label="活动描述" align="center" label-align="left" label-width="100">{{ list[0].activityDesc }}</el-descriptions-item>
      <el-descriptions-item label="客户名称" align="center" label-align="left" label-width="100">{{ list[0].username }}</el-descriptions-item>
      <el-descriptions-item label="车牌号" align="center" label-align="left" label-width="100">
        <el-tooltip placement="top">
          <template #content>
            <span v-for="(carNumber, index) in list[0].carNumber" :key="index">{{ carNumber }}<br /></span>
          </template>
          <div class="ellipsis">
            <el-tag v-for="(carNumber, index) in list[0].carNumber" :key="index" class="mr-5px">
              {{ carNumber }}
            </el-tag>
          </div>
        </el-tooltip>
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>

  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['erp:activity-user:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column fixed label="序号" align="center" width="65">
        <template #default="scope">
          <span>{{ (queryParams.pageNo - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="activityMoney" />
      <el-table-column label="返现、退货金额" align="center" prop="useMoney" />
      <el-table-column label="剩余金额" align="center" prop="ceilMoney" />
      <el-table-column label="取货时间" align="center" prop="pickUpTime" :formatter="dateFormatter2" />
      <el-table-column label="取货描述" align="center" prop="pickUpDesc" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:activity-user:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:activity-user:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ActivityUserForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { ActivityUserApi, ActivityUserVO } from '@/api/erp/activityuser'
import {dateFormatter2} from "@/utils/formatTime";
import ActivityUserForm from "@/views/erp/activityuser/ActivityUserForm.vue";

defineOptions({ name: 'JoinList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ActivityUserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const { query } = useRoute() // 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  activityId: query.id as unknown as number,
  userId: query.customerId as unknown as number
})

const getList = async () => {
  loading.value = true

  try {
    const data = await ActivityUserApi.getActivityUser(queryParams)
    console.log(data)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, queryParams.activityId, queryParams.userId, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ActivityUserApi.deleteActivityUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

onMounted(() => {
  getList()
})
</script>
